package com.example.tencent_cos_flutter;

import com.tencent.qcloud.core.auth.BasicLifecycleCredentialProvider;
import com.tencent.qcloud.core.auth.QCloudLifecycleCredentials;
import com.tencent.qcloud.core.auth.SessionQCloudCredentials;
import com.tencent.qcloud.core.common.QCloudClientException;

public class MySessionCredentialProvider extends BasicLifecycleCredentialProvider {

    String tmpSecretId = ""; // 临时密钥 SecretId
    String tmpSecretKey = ""; // 临时密钥 SecretKey
    String sessionToken = ""; // 临时密钥 Token
    long expiredTime = 0;//临时密钥有效截止时间戳，单位是秒
    //建议返回服务器时间作为签名的开始时间，避免由于用户手机本地时间偏差过大导致请求过期
    // 返回服务器时间作为签名的起始时间
    long startTime = 0; //临时密钥有效起始时间，单位是秒

    public MySessionCredentialProvider(String tmpSecretId, String tmpSecretKey, String sessionToken,long startTime, long expiredTime) {
        this.tmpSecretId = tmpSecretId;
        this.tmpSecretKey = tmpSecretKey;
        this.sessionToken = sessionToken;
        this.expiredTime = expiredTime;
        this.startTime = startTime;
    }

    @Override
    protected QCloudLifecycleCredentials fetchNewCredentials() throws QCloudClientException {


        // 最后返回临时密钥信息对象
        return new SessionQCloudCredentials(tmpSecretId, tmpSecretKey,
                sessionToken, startTime, expiredTime);

    }
}
